package cn.jbolt.ec.markert;

import cn.jbolt.base.JBoltBaseService;
import cn.jbolt.common.model.BecMarketingRetailer;

import cn.jbolt.common.model.BecRetailer;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

public class MarketRetailerService extends JBoltBaseService<BecMarketingRetailer> {
    @Override
    protected BecMarketingRetailer dao() {
        return  new BecMarketingRetailer().dao();
    }


    /**
     * 获取营销管理首页
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @param retailer_id
     * @return
     */
    public Page<BecMarketingRetailer> get_market_retailer_page(Integer pageNum,Integer pageSize,String keywords,Integer retailer_id ){

        Kv conf = Kv.by("table",table());
        conf.setIfNotNull("keywords",keywords);
        conf.setIfNotNull("retailer_id",retailer_id);
        return  paginateBySqlTemplate("ec.mall.manage.market.get_market_retailer_page",conf,pageNum,pageSize);


    }


    /**
     * 保存新添的 营销管理
     * @param becMarketingRetailer
     * @return
     */
    public Ret save_market_retailer(BecMarketingRetailer becMarketingRetailer){

        if (becMarketingRetailer == null || notOk(becMarketingRetailer.getRetailerId())||
        notOk(becMarketingRetailer.getCommodityTypeId()))
            return fail("传入参数错误！");

        boolean success = becMarketingRetailer.save();
        if (success)
            return success("操作成功");
        return fail("操作失败");

    }


    /**
     * 保存 修改 的 营销管理信息
     * @param becMarketingRetailer
     * @return
     */
    public Ret update_market_retailer(BecMarketingRetailer becMarketingRetailer){
        return update_market(becMarketingRetailer);
    }


    /**
     * 设置  营销转态
     * status = 3  时  删除
     * status = 1  时  转换 营销的 有效和暂存状态
     * @param market_id
     * @param status
     * @return
     */
    public  Ret set_market_status(Integer market_id , Integer status){
        if (notOk(market_id) || notOk(status))
            return  fail("参数传入错误!");
        BecMarketingRetailer becMarketingRetailer = findById(market_id);
        if (becMarketingRetailer == null)
            return fail("操作失败,数据错误");

        if (status == 3){
            if (becMarketingRetailer.getStatus() == 2)
                return fail("有效营销不能删除");
            becMarketingRetailer.setStatus(status);
        }

        if (status == 1){
            if (becMarketingRetailer.getStatus()==1){
                becMarketingRetailer.setStatus(2);
            }

            else if(becMarketingRetailer.getStatus() == 2){
                becMarketingRetailer.setStatus(1);
            }
        }

        return update_market(becMarketingRetailer);

    }


    public Ret update_market(BecMarketingRetailer becMarketingRetailer){
        if (becMarketingRetailer == null)
         return fail("传入参数错误！");
        boolean success =  becMarketingRetailer.update();
        if(success)
            return success("操作成功");
        return fail("操作失败");
    }

}
